package com.orange.common.validation;

import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * 参数校验配置类
 *
 * @Author YWF
 * @Date 2025/5/8 16:36
 */
@Configuration
public class ValidatorConfig {

    /**
     * 开启快速失败校验，有且仅有一个字段校验失败，立即返回
     * @return
     */
    @Bean
    public Validator validator() {
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                .configure()
                // 快速失败模式
                .failFast(false) // true 打开快速检验，false 关闭，也可以通过配置参数去配置
                .buildValidatorFactory();
        return validatorFactory.getValidator();
    }

}
